package ru.simplemaps.osmapi.objects;

import java.util.HashMap;
import java.util.Map;

/**
 * Base class for OSM primitives: Node, Way, Relation
 *
 * @author enaku_adm
 * @since 22.07.2010 11:15:51
 */
public class AbstractOSMElement {
    private Map<String, String> tags;
    public final long id;
    public OSMElementType type;

    protected AbstractOSMElement(long id, OSMElementType type) {
        this.id = id;
        this.type = type;
    }

    public synchronized Map<String, String> getTags() {
        if (tags == null) {
            tags = new HashMap<String, String>();
        }

        return tags;
    }

    public void setTag(String k, String v) {
        getTags().put(k, v);
    }

    public void getTag(String k) {
        getTags().get(k);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof AbstractOSMElement)) return false;

        AbstractOSMElement that = (AbstractOSMElement) o;

        if (id != that.id) return false;
        if (type != that.type) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = (int) (id ^ (id >>> 32));
        result = 31 * result + type.hashCode();
        return result;
    }

    public boolean equalsFull(Object o) {
        boolean simple = equals(o);
        if (!simple) return false;

        AbstractOSMElement other = (AbstractOSMElement) o;
        return getTags().equals(other.getTags());
    }
}
